package com.KKB.dao.impl;

import com.KKB.bean.Role;
import com.KKB.bean.User;
import com.KKB.dao.UserDao;
import com.KKB.util.DBUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl extends DBUtils implements UserDao {
    @Override
    public User login(String username, String password) {
        User user=null;
        try {
            String sql="select * from user where loginName=? and password=?";
            ArrayList<Object> list = new ArrayList<>();
            list.add(username);
            list.add(password);
            resultSet = query(sql, list);
            if (resultSet==null){
                return null;
            }
            while (resultSet.next()){
                user=new User();
                user.setLoginName(username);
                user.setRealName(resultSet.getString("realname"));
                user.setUserId(resultSet.getInt("userid"));
                user.setRoleId(resultSet.getInt("roleid"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }

        return user;
    }

    @Override
    public List<User> getUserList(int pageIndex, int pageSize) {
        List<User> usersList=new ArrayList<>();
        try {
            String sql="select userid,loginname,realname,rolename from user u,role r where u.roleid=r.roleid  limit ?,?";
            List params=new ArrayList();
            params.add((pageIndex-1)*pageSize);
            params.add(pageSize);
            resultSet = query(sql, params);

           while (resultSet.next()){
               User user=new User();
               user.setUserId(resultSet.getInt("userid"));
               user.setLoginName(resultSet.getString("loginname"));
               user.setRealName(resultSet.getString("realname"));

               Role role=new Role();
               role.setRoleName(resultSet.getString("rolename"));

               user.setRole(role);
               usersList.add(user);
           }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return  usersList;
    }



    @Override
    public int total() {
        int total = 0;
        try {
            String sql = " select count(1) from user u,role r where u.roleid = r.roleid ";
            query(sql, null);
            while (resultSet.next()) {
                total = resultSet.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return total;
    }

    @Override
    public int addUsers(User user) {
        int update = 0;
        try {
            String sql = " insert into user values(null,?,?,?,?,?,?,?,?,?,?) ";
            List params = new ArrayList();
            params.add(user.getLoginName());
            params.add(user.getPassword());
            params.add(user.getRealName());
            params.add(user.getSex());
            params.add(user.getEmail());
            params.add(user.getPhone());
            params.add(user.getAddress());
            params.add(user.getCardId());
            params.add(user.getDesc());
            params.add(user.getRoleId());
            update = update(sql, params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }

        return update;
    }
    @Override
    public User findById(int uid) {
        User user = new User();
        try {
            String sql = " select * from user where userid=? ";
            List params = new ArrayList();
            params.add(uid);
            query(sql, params);

            while (resultSet.next()) {
                user.setUserId(uid);
                user.setLoginName(resultSet.getString("loginName"));
                user.setPassword(resultSet.getString("password"));
                user.setRealName(resultSet.getString("realname"));
                user.setSex(resultSet.getInt("sex"));
                user.setEmail(resultSet.getString("email"));
                user.setPhone(resultSet.getString("phone"));
                user.setAddress(resultSet.getString("address"));
                user.setCardId(resultSet.getString("cardid"));
                user.setDesc(resultSet.getString("desc"));
                user.setRoleId(resultSet.getInt("roleid"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }

        return user;
    }

    @Override
    public int update(User user) {
        int update = 0;
        try {
            String sql = " update user set loginName=?,password=?,realname=?,sex=?,email=?,phone=?,address=?,cardid=?,`desc`=?,roleid=? where userid=? ";
            List params = new ArrayList();
            params.add(user.getLoginName());
            params.add(user.getPassword());
            params.add(user.getRealName());
            params.add(user.getSex());
            params.add(user.getEmail());
            params.add(user.getPhone());
            params.add(user.getAddress());
            params.add(user.getCardId());
            params.add(user.getDesc());
            params.add(user.getRoleId());
            params.add(user.getUserId());
            update = update(sql, params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }


        return update;
    }


    @Override
    public int deleteById(int uid) {
        int update = 0;
        try {
            String sql = " delete from user where userid=? ";
            List params = new ArrayList();
            params.add(uid);
            update = update(sql, params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }

        return update;
    }
}
